--[[	$Id: actionBars.lua 3945 2014-10-23 14:00:04Z sdkyron@gmail.com $	]]

local dummy, kill, pixelScale = caelUI.dummy, caelUI.kill, caelUI.scale

---------------------------------------------
-- Local variable settings for action bars.
---------------------------------------------

local actionBar = {
	["settings"] = {
		["showGrid"]				= true,
		["showPetGrid"] 			= true,

		["BarMouseover1"]			= false,
		["BarMouseover2"]			= false,
		["BarMouseover3"]			= false,
		["BarMouseover4"]			= false,
		["BarMouseover5"]			= true,

		["mouseOverPetBar"]			= true,
		["mouseOverShapeshiftBar"]	= false,

		["showBar1"]				= true,
		["showBar2"]				= true,
		["showBar3"]				= true,
		["showBar4"]				= true,
		["showBar5"]				= true
	}
}

-- Global variable
local _G = getfenv(0)

-----------------------------------------------
-- Hide default Blizzard frames we don't need
-----------------------------------------------

local frames = {
	MainMenuBar, MainMenuBarArtFrame, OverrideActionBar,
	PossessBarFrame, IconIntroTracker, -- PetActionBarFrame
	ShapeshiftBarLeft, ShapeshiftBarMiddle, ShapeshiftBarRight,
	StanceBarLeft, StanceBarMiddle, StanceBarRight,
}

for _, frame in pairs(frames) do
	if frame:GetObjectType() == "Texture" then
		frame:SetTexture("")
	else
		frame:UnregisterAllEvents()
		frame.ignoreFramePositionManager = true
		frame:SetParent(caelUI.KillFrame)
	end
end

MainMenuBar.slideOut.IsPlaying = function()
	return true
end

for i = 1, 6 do
	local button = _G["OverrideActionBarButton"..i]
	button:UnregisterAllEvents()
	button:SetAttribute("statehidden", 1)
end

hooksecurefunc("TalentFrame_LoadUI", function()
	PlayerTalentFrame:UnregisterEvent("ACTIVE_TALENT_GROUP_CHANGED")
end)

hooksecurefunc("ActionButton_OnEvent", function(self, event, ...)
	if event == "PLAYER_ENTERING_WORLD" then
		self:UnregisterEvent("ACTIONBAR_SHOWGRID")
		self:UnregisterEvent("ACTIONBAR_HIDEGRID")
		self:UnregisterEvent("PLAYER_ENTERING_WORLD")
	end
end)

---------------------------------
-- Toggle for mouseover on bars
---------------------------------

local BarMouseover = function(panel, bar, button, alpha)
	if bar ~= nil then
		bar:SetAlpha(alpha)
	end

	if panel ~= nil then
		panel:SetAlpha(alpha)
	end

	if button ~= nil then
		for i = 1, 12 do
			_G[button .. i]:SetAlpha(alpha)
		end
	end
end

----------------------
-- Setup button grid
----------------------

local GetButton = function(id)
	if id <= 12 then
		return _G["ActionButton"..id]
	elseif id <= 24 then
		return _G["MultiBarBottomLeftButton"..(id - 12)]
	elseif id <= 36 then
		return _G["MultiBarBottomRightButton"..(id - 24)]
	elseif id <= 48 then
		return _G["MultiBarRightButton"..(id - 36)]
	elseif id <= 60 then
		return _G["MultiBarLeftButton"..(id - 48)]
	end
end

local buttonGrid = CreateFrame("Frame")
buttonGrid:RegisterEvent("PLAYER_ENTERING_WORLD")
buttonGrid:SetScript("OnEvent", function(self, event)
	self:UnregisterEvent("PLAYER_ENTERING_WORLD")

	SetActionBarToggles(actionBar["settings"].showBar2, actionBar["settings"].showBar3, actionBar["settings"].showBar4, actionBar["settings"].showBar5)

	if actionBar["settings"].showGrid == true then
		for i = 1, 12 do
			local button = GetButton(i)
			button:SetAttribute("showgrid", 1)
			button:SetParent(_G["uiActionBar1"])
			ActionButton_ShowGrid(button)
		end
	end

	PetActionBarFrame:UnregisterEvent("PET_BAR_SHOWGRID")
	PetActionBarFrame:UnregisterEvent("PET_BAR_HIDEGRID")

	if actionBar["settings"].showPetGrid == true then
--		PetActionBar_ShowGrid()
	end
end)

---------------------------------------------------
-- BAR 1
---------------------------------------------------

local bar1 = CreateFrame("Frame", "bar1", caelPanel5, "SecureHandlerStateTemplate")
bar1:ClearAllPoints()
bar1:SetAllPoints(caelPanel5)

local barPage = {
	["DEFAULT"] = "[vehicleui:12] 12; [possessbar] 12; [shapeshift] 13; [overridebar] 14; [bar:2] 2; [bar:3] 3; [bar:4] 4; [bar:5] 5; [bar:6] 6;", 
	["DRUID"] = "[bonusbar:1,nostealth] 7; [bonusbar:1,stealth] 8; [bonusbar:2] 8; [bonusbar:3] 9; [bonusbar:4] 10;",
	["MONK"] = "[bonusbar:1] 7; [bonusbar:2] 8; [bonusbar:3] 9;",
	["PRIEST"] = "[bonusbar:1] 7;",
	["ROGUE"] = "[bonusbar:1] 7; [stance:3] 7;",
	["WARLOCK"] = "[stance:2] 10;",
	["WARRIOR"] = "[bonusbar:1] 7; [bonusbar:2] 8; [bonusbar:3] 9;",
}

local function GetBar()
	local condition = barPage["DEFAULT"]
	local class = caelUI.playerClass
	local page = barPage[class]

	if page then
		condition = condition.." "..page
	end

	condition = condition.." 1"

	return condition
end

bar1:RegisterEvent("PLAYER_LOGIN")
bar1:RegisterEvent("PLAYER_ENTERING_WORLD")
bar1:RegisterEvent("KNOWN_CURRENCY_TYPES_UPDATE")
bar1:RegisterEvent("CURRENCY_DISPLAY_UPDATE")
bar1:RegisterEvent("BAG_UPDATE")
bar1:RegisterEvent("ACTIVE_TALENT_GROUP_CHANGED")
bar1:SetScript("OnEvent", function(self, event, ...)
	if event == "PLAYER_LOGIN" or event == "ACTIVE_TALENT_GROUP_CHANGED" then
		local button
		for i = 1, NUM_ACTIONBAR_BUTTONS do
			button = _G["ActionButton"..i]
			self:SetFrameRef("ActionButton"..i, button)
		end	

		self:Execute([[
			buttons = table.new()
			for i = 1, 12 do
				table.insert(buttons, self:GetFrameRef("ActionButton"..i))
			end
		]])

		self:SetAttribute("_onstate-page", [[ 
			for i, button in ipairs(buttons) do
				button:SetAttribute("actionpage", tonumber(newstate))
			end
		]])

		RegisterStateDriver(self, "page", GetBar())
	elseif event == "PLAYER_ENTERING_WORLD" then
		local button
		for i = 1, 12 do
			button = _G["ActionButton"..i]
			button:SetScale(0.68)
			button:ClearAllPoints()
			button:SetParent(self)
			if i == 1 then
				button:SetPoint("TOPLEFT", caelPanel5, pixelScale(4.5), pixelScale(-4.5))
			elseif i == 7 then
				button:SetPoint("TOPLEFT", _G["ActionButton1"], "BOTTOMLEFT", 0, pixelScale(-6.5))
			else
				button:SetPoint("LEFT", _G["ActionButton"..i-1], "RIGHT", pixelScale(5), 0)
			end
		end
	else
		MainMenuBar_OnEvent(self, event, ...)
	end
end)

------------------------------
-- BAR 2, BAR 3, BAR 4, BAR 5
------------------------------

-- Bar2 = caelPanel6 = MultiBarBottomLeft
-- Bar3 = caelPanel7 = MultiBarBottomRight
-- Bar4 = caelPanel4 = MultiBarRight
-- Bar5 = caelPanel9 = MultiBarLeft

local bar2 = CreateFrame("Frame", "bar2", caelPanel6)
local bar3 = CreateFrame("Frame", "bar3", caelPanel7)
local bar4 = CreateFrame("Frame", "bar4", UIParent)
local bar5 = CreateFrame("Frame", "bar5", UIParent)

do 
	local bars = {{}, {}, {}}

	bars[1] = {
		["panel"] = caelPanel6,
		["button"] = "MultiBarBottomLeftButton",
		["barFrame"] = bar2,
		["realBar"] = MultiBarBottomLeft,
		["barNum"] = 2,
	}
	bars[2] = {
		["panel"] = caelPanel7,
		["button"] = "MultiBarBottomRightButton",
		["barFrame"] = bar3,
		["realBar"] = MultiBarBottomRight,
		["barNum"] = 3,
	}
	bars[3] = {
		["panel"] = caelPanel4,
		["button"] = "MultiBarRightButton",
		["barFrame"] = bar4,
		["realBar"] = MultiBarRight,
		["barNum"] = 4,
	}
	bars[4] = {
		["panel"] = caelPanel9,
		["button"] = "MultiBarLeftButton",
		["barFrame"] = bar5,
		["realBar"] = MultiBarLeft,
		["barNum"] = 5,
	}

	for _, bar in ipairs(bars) do
		local button1 = _G[bar.button .. "1"]

		-- Set the bar frame to the panel cael panel it is tied to.
		bar.barFrame:SetAllPoints(bar.panel)

		-- Set the Blizzard bar parent.
		bar.realBar:SetParent(bar.barFrame)

		for i = 1, 12 do
			local button = _G[bar.button .. i]
			local buttonPrev = _G[bar.button .. i - 1]

			button:ClearAllPoints()
			button:SetScale(0.68)

			if i == 1 then
				button:SetPoint("TOPLEFT", bar.panel, pixelScale(4.5), pixelScale(-4.5))
			elseif bar.barNum == 5 then
				button:SetPoint("TOPLEFT", buttonPrev, "BOTTOMLEFT", 0, pixelScale(-4.5))
			elseif i == 7 then
				button:SetPoint("TOPLEFT", button1, "BOTTOMLEFT", 0, pixelScale(-6.5))
			else
				button:SetPoint("LEFT", buttonPrev, "RIGHT", pixelScale(5), 0)
			end

			-- mouse over enable
			if (actionBar["settings"]["showBar" .. bar.barNum] and actionBar["settings"]["BarMouseover" .. bar.barNum]) then
				button:SetScript("OnEnter", function() BarMouseover(bar.panel, bar.realBar, bar.button, 1) end)
				button:SetScript("OnLeave", function() BarMouseover(bar.panel, bar.realBar, bar.button, 0) end)
				BarMouseover(bar.panel, bar.realBar, bar.button, 0)
			end
		end

		if not actionBar["settings"]["showBar" .. bar.barNum] then
			bar.panel:Hide()
			bar.realBar:Hide()
			bar.barFrame:Hide()
		elseif actionBar["settings"]["BarMouseover" .. bar.barNum] == true then
			bar.panel:EnableMouse(true)
			bar.panel:SetScript("OnEnter", function() BarMouseover(bar.panel, bar.realBar, bar.button, 1) end)
			bar.panel:SetScript("OnLeave", function() BarMouseover(bar.panel, bar.realBar, bar.button, 0) end)
			BarMouseover(bar.panel, bar.realBar, bar.button, 0)
		end
	end
end

---------------------
-- EXTRA ACTION BAR
---------------------

local barExtra = CreateFrame("Frame", "barExtra", UIParent, "SecureHandlerStateTemplate")
barExtra:SetSize(ExtraActionBarFrame:GetSize())
barExtra:SetPoint("BOTTOM", caelPanel1, "TOP", 0, pixelScale(26))

RegisterStateDriver(barExtra, "visibility", "[petbattle][overridebar][vehicleui] hide; show")

ExtraActionBarFrame:SetParent(barExtra)
ExtraActionBarFrame:EnableMouse(false)

ExtraActionBarFrame:ClearAllPoints()
ExtraActionBarFrame:SetPoint("CENTER")
ExtraActionBarFrame.ignoreFramePositionManager = true

-------------------
-- SHAPESHIFT BAR
-------------------

local barShift = CreateFrame("Frame", "barShift", UIParent)
barShift:ClearAllPoints()
barShift:SetPoint("BOTTOMLEFT", bar1, "TOPLEFT",  pixelScale(3), pixelScale(-4))
barShift:SetSize(StanceBarFrame:GetSize())

RegisterStateDriver(barShift, "visibility", "[petbattle][overridebar][vehicleui][possessbar,@vehicle,exists] hide; show")

StanceBarFrame:SetParent(barShift)
StanceBarFrame:EnableMouse(false)

for i = 1, NUM_STANCE_SLOTS do
	local button = _G["StanceButton" .. i]
	local buttonPrev = _G["StanceButton" .. i - 1]
	button:ClearAllPoints()
	button:SetSize(ActionButton1:GetSize())
	button:SetScale(0.68)
	if i == 1 then
		button:SetPoint("BOTTOMLEFT", barShift, 0, pixelScale(6.5))
	else
		button:SetPoint("LEFT", buttonPrev, "RIGHT", pixelScale(6), 0)
	end
end

------------
-- PET BAR
------------

local petBar = CreateFrame("Frame", "petBar", UIParent, "SecureHandlerStateTemplate")
petBar:ClearAllPoints()
petBar:SetWidth(pixelScale(120))
petBar:SetHeight(pixelScale(50))
petBar:SetPoint("BOTTOM", UIParent, pixelScale(-337), pixelScale(360))

RegisterStateDriver(petBar, "visibility", "[petbattle][overridebar][vehicleui][possessbar,@vehicle,exists] hide; [@pet,exists] show; hide")

PetActionBarFrame:SetParent(petBar)
PetActionBarFrame:EnableMouse(false)

-- function to toggle the display of the pet bar
local PetBarToggle = function(alpha)
	for i = 1, NUM_PET_ACTION_SLOTS do
		local button = _G["PetActionButton" .. i]
		button:SetAlpha(alpha)
	end
end

if actionBar["settings"].mouseOverPetBar == true then
	petBar:EnableMouse(true)
	petBar:SetScript("OnEnter", function(self) PetBarToggle(1) end)
	petBar:SetScript("OnLeave", function(self) PetBarToggle(0) end)
end

local button1 = _G["PetActionButton1"]
for i = 1, NUM_PET_ACTION_SLOTS do
	local button = _G["PetActionButton"..i]
	local buttonPrev = _G["PetActionButton"..i - 1]

	button:ClearAllPoints()
	button:SetParent(petBar)
	button:SetScale(0.77) 

	if i == 1 then
		button:SetPoint("TOPLEFT", petBar, pixelScale(1), pixelScale(-1))
	elseif i == ((NUM_PET_ACTION_SLOTS / 2) + 1) then -- Get our middle button + 1 to make the rows even
		button:SetPoint("TOPLEFT", button1, "BOTTOMLEFT", 0, pixelScale(-1))
	else
		button:SetPoint("LEFT", buttonPrev, "RIGHT", pixelScale(1), 0)
	end

	if actionBar["settings"].mouseOverPetBar == true then
		button:SetAlpha(0)
		button:HookScript("OnEnter", function(self) PetBarToggle(1) end)
		button:HookScript("OnLeave", function(self) PetBarToggle(0) end)
	end
end

--------------
-- TOTEMS BAR
--------------
local totemBar = _G["MultiCastActionBarFrame"]

if totemBar then
	totemBar:SetScript("OnUpdate", nil)
	totemBar:SetScript("OnShow", nil)
	totemBar:SetScript("OnHide", nil)
	totemBar:SetParent(caelPanel3)
	totemBar:ClearAllPoints()
	totemBar:SetPoint("BOTTOM", caelPanel3, "TOP", 0, caelUI.scale(5))
	totemBar:SetScale(0.85)

	hooksecurefunc("MultiCastActionButton_Update", function(self)
		if not InCombatLockdown() then
			self:SetAllPoints(self.slotButton)
		end
	end)
end

------------
-- VEHICLE
------------

-- Vehicle button
local vehicleExitButton = CreateFrame("BUTTON", nil, UIParent, "SecureActionButtonTemplate")

vehicleExitButton:SetSize(pixelScale(33), pixelScale(33))
vehicleExitButton:SetPoint("BOTTOM", pixelScale(-146), pixelScale(263))

vehicleExitButton:RegisterForClicks("AnyUp")
vehicleExitButton:SetScript("OnClick", function() VehicleExit() end)

vehicleExitButton:SetNormalTexture([[Interface\Vehicles\UI-Vehicles-Button-Exit-Up]])
vehicleExitButton:SetPushedTexture([[Interface\Vehicles\UI-Vehicles-Button-Exit-Down]])
vehicleExitButton:SetHighlightTexture([[Interface\Vehicles\UI-Vehicles-Button-Exit-Down]])

vehicleExitButton:RegisterEvent("UNIT_ENTERING_VEHICLE")
vehicleExitButton:RegisterEvent("UNIT_ENTERED_VEHICLE")
vehicleExitButton:RegisterEvent("UNIT_EXITING_VEHICLE")
vehicleExitButton:RegisterEvent("UNIT_EXITED_VEHICLE")
vehicleExitButton:RegisterEvent("ZONE_CHANGED_NEW_AREA")
vehicleExitButton:SetScript("OnEvent", function(self, event, arg1)
	if (((event == "UNIT_ENTERING_VEHICLE") or (event == "UNIT_ENTERED_VEHICLE"))
		and arg1 == "player") then
		vehicleExitButton:SetAlpha(1)
	elseif (
		(
		(event == "UNIT_EXITING_VEHICLE") or (event == "UNIT_EXITED_VEHICLE")
		) and
		arg1 == "player") or (
		event == "ZONE_CHANGED_NEW_AREA" and not UnitHasVehicleUI("player")
		) then
		vehicleExitButton:SetAlpha(0)
	end
end)

vehicleExitButton:SetAlpha(0)